package cn.hust.springboot.controller;

import cn.hust.springboot.bean.Emp;
import cn.hust.springboot.config.PaginationConstant;
import cn.hust.springboot.service.impl.ExportDataServiceImpl;
import cn.hust.springboot.service.IEmpService;
import cn.hust.springboot.service.impl.PersonnelServiceImpl;
import cn.hust.springboot.bean.ColumnTitleMap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc:数据导出api控制器
 * @author: chao
 * @time: 2022.7.1
 */

@Controller
public class ExportDataController {
    @Autowired
    IEmpService empService;
    @Autowired
    PersonnelServiceImpl personnelService;
    @Autowired
    ExportDataServiceImpl exportDataService;


    // 查询所有员工返回列表页面
    @GetMapping("/reports")
    public String list(Model model, @RequestParam(value="pageNum",defaultValue="1") Integer pageNum) {
//        List<Emp> emps = empService.getAllEmps();
//        List<Emp> emps = empService.getEmpAndDept();

        // 放在请求域中
//        model.addAttribute("emps",emps);
        // thymleaf默认就会拼串
        // classpath:/templates/xxx.html
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        //设置分页(当前页，和每页显示数据条数)
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);

        //查找数据
        List<Emp> reportTable = empService.selectReport();
        //将查找出的结果封装到PageInfo对象中，这个对象包含了 emps集合和关于分页的方法，如下
        //pageInfo.getPageNum();
        //pageInfo.getPages(); 得到总页数
        //pageInfo.getNextPage(); 得到下一页
        //pageInfo.getPrePage(); 得到前一页
        PageInfo<Emp> pageInfo=new PageInfo<>(reportTable);
        // model.addAttribute("emps",emps);
        //用Model传递对象到page页面
        model.addAttribute("pageInfo",pageInfo);
        return "report/report";
    }


    @GetMapping(value = "/reports/excel5")
    public void getPersonnelEx(
            HttpServletResponse response
    ) {
        try {
            List<Map<String,Object>> userList = personnelService.queryPersonnelResultListMap();
            ArrayList<String> titleKeyList= new ColumnTitleMap("emp").getTitleKeyList();
            Map<String, String> titleMap = new ColumnTitleMap("emp").getColumnTitleMap();
            exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
        } catch (Exception e) {
            //
            System.out.println(e.toString());
        }
    }


    @GetMapping(value = "/reports/excel4")
    public void getPersonnelEx4(
            HttpServletResponse response
    ) {
        try {
            List<Map<String,Object>> userList = personnelService.ResultListMapOfPosition();
            ArrayList<String> titleKeyList= new ColumnTitleMap("emp").getTitleKeyList();
            Map<String, String> titleMap = new ColumnTitleMap("emp").getColumnTitleMap();
            exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
        } catch (Exception e) {
            //
            System.out.println(e.toString());
        }
    }


    @GetMapping(value = "/reports/excel3")
    public void getPersonnelEx3(
            HttpServletResponse response
    ) {
        try {
            List<Map<String,Object>> userList = personnelService.ResultListMapOfDept();
            ArrayList<String> titleKeyList= new ColumnTitleMap("emp").getTitleKeyList();
            Map<String, String> titleMap = new ColumnTitleMap("emp").getColumnTitleMap();
            exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
        } catch (Exception e) {
            //
            System.out.println(e.toString());
        }
    }


    @GetMapping(value = "/reports/excel2")
    public void getPersonnelEx2(
            HttpServletResponse response
    ) {
        try {
            List<Map<String,Object>> userList = personnelService.ResultListMapOfLeave();
            ArrayList<String> titleKeyList= new ColumnTitleMap("emp").getTitleKeyList();
            Map<String, String> titleMap = new ColumnTitleMap("emp").getColumnTitleMap();
            exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
        } catch (Exception e) {
            //
            System.out.println(e.toString());
        }
    }


    @GetMapping(value = "/reports/excel1")
    public void getPersonnelEx1(
            HttpServletResponse response
    ) {
        try {
            List<Map<String,Object>> userList = personnelService.ResultListMapOfEnter();
            ArrayList<String> titleKeyList= new ColumnTitleMap("emp").getTitleKeyList();
            Map<String, String> titleMap = new ColumnTitleMap("emp").getColumnTitleMap();
            exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
        } catch (Exception e) {
            //
            System.out.println(e.toString());
        }
    }
}
